package com.heimali.happyreader;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.heimali.happyreader.config.CommonConfig;
import com.heimali.happyreader.config.Constants;
import com.heimali.happyreader.model.Array;
import com.heimali.happyreader.model.LoginUserInfo;
import com.heimali.happyreader.ui.QAccountEditText;
import com.heimali.happyreader.ui.QPasswordEditText;
import com.heimali.happyreader.utils.ErrorUtils;
import com.heimali.happyreader.utils.UserUtils;
import com.heimali.happyreader.wraper.BaseWraper;
import com.heimali.happyreader.wraper.UserCenterWraper;

public class RegisterActivity extends BaseActivity {

	public static final String TAG = "RegisterActivity";
	private Button rightButton;
	private Button registerButton;
	private QAccountEditText accountEditText;
	private QPasswordEditText passwordEditText;
	private RelativeLayout registerLayout;
	private ProgressDialog pd;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		initView();
	}

	private void initView() {
		rightButton = (Button) findViewById(R.id.right_btn);
		rightButton.setText(R.string.back_btn);
		rightButton.setOnClickListener(this);
		registerButton = (Button) findViewById(R.id.btn_regist);
		registerButton.setOnClickListener(this);

		registerLayout = (RelativeLayout) findViewById(R.id.register_layout);
		accountEditText = (QAccountEditText) findViewById(R.id.qaet_account);
		accountEditText.setSelectedCallback(new QAccountEditText.SelectedCallback() {
			public void run() {
				passwordEditText.setFocus();
			}
		});
		passwordEditText = (QPasswordEditText) findViewById(R.id.qpet_password);

		// 调整 AutoCompleteDropDownList width
		registerLayout.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
			public boolean onPreDraw() {
				accountEditText.setDropDownWidth(registerLayout.getMeasuredWidth());
				registerLayout.getViewTreeObserver().removeOnPreDrawListener(this);
				return true;
			}
		});
	}

	private void register() {
		String username = accountEditText.getText().toString().trim();
		int checkUserNameResult = UserUtils.checkUserName(username);
		if (checkUserNameResult != UserUtils.USERNAME_OK) {
			Toast.makeText(this, ErrorUtils.getMsg(checkUserNameResult), 0).show();
			return;
		}
		String password = passwordEditText.getText().toString();
		int checkPasswordResult = UserUtils.checkPassword(password);
		if (checkPasswordResult != UserUtils.PASSWORD_OK) {
			Toast.makeText(this, ErrorUtils.getMsg(checkPasswordResult), 0).show();
			return;
		}

		pd = new ProgressDialog(this);
		pd.setMessage(getString(R.string.logining));
		pd.setCancelable(false);
		pd.show();

		new UserCenterWraper(username, password, UserCenterWraper.TYPE.REGISTER, new BaseWraper.Callback() {

			public void onSuccess(Object result) {
				pd.dismiss();
				LoginUserInfo userInfo = (LoginUserInfo) result;
				if (userInfo == null || TextUtils.isEmpty(userInfo.uid) || TextUtils.isEmpty(userInfo.token)
						|| TextUtils.isEmpty(userInfo.username)) {
					return;
				}

				Statics.UID = userInfo.uid;
				Statics.TOKEN = userInfo.token;
				Statics.USERNAME = userInfo.username;
				Array userConfig = new Array(new String[] { Constants.COMMON_CONFIG_UID, userInfo.uid },
						new String[] { Constants.COMMON_CONFIG_TOKEN, userInfo.token }, new String[] {
								Constants.COMMON_CONFIG_USERNAME, userInfo.username });
				CommonConfig.getInstance().setConfig(userConfig);
				CommonConfig.getInstance().setConfig(Constants.COMMON_CONFIG_IS_LOGIN, true);

				Intent intent = new Intent(RegisterActivity.this, ArticleListActivity.class);
				startActivity(intent);
				accountEditText.statAccount();
				finish();
			}

			public void onFail(int errno, String errmsg) {
				pd.dismiss();
				String msg = (errmsg == null) ? ErrorUtils.getMsg(errno) : errmsg;
				Toast.makeText(RegisterActivity.this, msg, 0).show();
			}
		}).execute();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.right_btn:
			finish();
			break;
		case R.id.btn_regist:
			register();
			break;
		default:
			break;
		}
	}
}
